package com.cloud.school.bus.teacherhelper.modules.stuclass;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.SearchStudentRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.SearchStudentResponse;

/**
 * 增加学生，请输入学生全名和家长手机号
 * 
 * @author jiahongfei
 * 
 */
public class AddStudentNamePhoneActivity extends BaseActivity {

	private EditText mNameEditText;
	private EditText mParentPhoneEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_student_name_phone);

		init();
	}

	@Override
	protected void init() {

		mNameEditText = (EditText) findViewById(R.id.nameEditText);
		mParentPhoneEditText = (EditText) findViewById(R.id.parentPhoneEditText);

		setListener();
		setTitleNavBar();
	}

	@Override
	protected void setListener() {
		mNameEditText.setFilters(new InputFilter[] { new InputFilter() {

			@Override
			public CharSequence filter(CharSequence source, int start, int end,
					Spanned dest, int dstart, int dend) {

				if (source.equals("\n")) {
					return "";
				}
				return source;
			}

		} });
		mParentPhoneEditText.setFilters(new InputFilter[] { new InputFilter() {

			@Override
			public CharSequence filter(CharSequence source, int start, int end,
					Spanned dest, int dstart, int dend) {

				if (source.equals("\n")) {
					return "";
				}
				return source;
			}

		} });
		Button nextButton = (Button) findViewById(R.id.nextButton);
		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				StringBuilder stringBuilder = new StringBuilder();
				if (checkInfo(stringBuilder)) {
					searchStudent();
				} else {
					HandlerToastUI.getHandlerToastUI(mContext,
							stringBuilder.toString());
				}
			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(getString(R.string.add_student));
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private boolean checkInfo(StringBuilder stringBuilder) {
		stringBuilder.delete(0, stringBuilder.length());
		if (TextUtils.isEmpty(mNameEditText.getText().toString())) {
			stringBuilder.append(getString(R.string.enter_the_students_name));
			return false;
		} else if (TextUtils.isEmpty(mParentPhoneEditText.getText().toString())) {
			stringBuilder.append(getString(R.string.enter_the_parents_phone));
			return false;
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 添加学生返回
			switch (resultCode) {
			case RESULT_OK: {
				// 添加成功返回
				setResult(RESULT_OK);
				finish();
				break;
			}
			case RESULT_CANCELED: {

				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

	private void searchStudent() {
		NetworkClient.getNetworkClient()
				.GetRequest(
						new SearchStudentRequest(mContext, mNameEditText
								.getText().toString(), mParentPhoneEditText
								.getText().toString()),
						new SearchStudentResponse() {
							@Override
							public void onResponse(
									ResponseHandlerInterface responseHandlerInterface) {
								if ("1".equals(code)) {
									HandlerPostUI
											.getHandlerPostUI(new Runnable() {

												@Override
												public void run() {
													if (0 == studentList.size()) {
														// 没有找到
														// 手机号和姓名是新添加的
														Intent intent = new Intent( mContext,
																AddStudentDetailInfoActivity.class);
														intent.putExtra( "name", mNameEditText 
																.getText() .toString());
														intent.putExtra( "parentPhone", mParentPhoneEditText 
																.getText() .toString());
														startActivityForResult( intent, 0);

													} else {
														// 找到已经有的学生
														// 已经有手机号和姓名
														boolean showNewStudentButton = true;
														if(3 == result){
															showNewStudentButton = false;
														}
														 Intent intent = new Intent(mContext,
														 AddStudentExistActivity.class);
														 intent.putExtra("name",
														 mNameEditText.getText().toString());
														 intent.putExtra("parentPhone",
														 mParentPhoneEditText.getText().toString());
														 intent.putExtra("StudentList", (ArrayList<Student>)studentList);
														 intent.putExtra("ShowNewStudent", showNewStudentButton);
														 startActivityForResult(intent, 0);
													}
												}
											});
									DebugLog.logI(studentList.toString());
								} else if ("-3".equals(code)) {
									HandlerToastUI.getHandlerToastUI(mContext,
											"输入不能为空");
								} else if ("-2".equals(code)) {
									HandlerToastUI.getHandlerToastUI(mContext,
											"手机号格式不正确");
								}
							}
						});
	}

}
